$(function () {
    loadList();
});
var path = "localhost:9102/";


//加载列表
function loadList() {
    $.ajax({
        type: "GET",
        url: "/api/post/count",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'loadPage', //注意，这里的 loadPage 是 ID，不用加 # 号
                    count: data, //数据总数，从服务端得到
                    curr: 1,
                    limit: 5,//每页条数
                    jump: function (obj, first) {
                        //obj包含了当前分页的所有参数，比如：
                        //得到当前页，以便向服务端请求对应页的数据。console.log(obj.curr);
                        //得到每页显示的条数 console.log(obj.limit);
                        $.ajax({
                            type: "GET",
                            url: "/front/blog/postIndex?page=" + obj.curr + '&limit=' + obj.limit,
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                console.log(data);
                                var indexblog = '';
                                if (data.code === "200") {
                                    $("#home").empty();
                                    for (var i = 0; i < data.data.records.length; i++) {
                                        categorys = data.data.records[i].category;
                                        if (data.data.records[i].postThumbnail === "") {
                                            //纯文字版本
                                            indexblog =
                                                '<div class="home-article-wrapper clearfix">' +
                                                '<div class="home-article" data-cover="false">' +
                                                '<div class="home-article-inner" data-cover="false">' +
                                                '<a class="passage-meta">' +
                                                '<span>' +
                                                '<i class="fa fa-calendar"></i>' + fmt(data.data.records[i].postDate) + '' +
                                                '</span>' +
                                                '| <i class="fa fa-location-arrow"></i><a href="/web/categoryPage?categoryId=' + categorys.cateId + '"> ' + categorys.cateName + '' +
                                                '</a></span>' +
                                                '<span>' +
                                                '| <i class="fa fa-unlock-alt"></i>UNLOCK' +
                                                '</span>' +
                                                // '</div>' +
                                                '<h1 class="home-article-title">' +
                                                '<a href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' + data.data.records[i].postTitle + '</a>' +
                                                '</h1>' +
                                                '<div class="home-article-content passage-article">' +
                                                '' + data.data.records[i].postContent.substring(0, 200) + '' +
                                                '</div>' +
                                                '<a class="home-article-read" href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' +
                                                'README MORE >>>' +
                                                '</a>' +
                                                // '<div class="passage-tags">' +
                                                // '<a href="/tags/第三方库/"><i class="fa fa-tags"></i>第三方库</a>' +
                                                // '</div>' +
                                                '</div>' +
                                                '</div>';
                                            $("#home").append(indexblog);
                                        }
                                        if (data.data.records[i].postThumbnail !== "") {
                                            //有图版本
                                            //var articleImg = data.data.records[i].postThumbnail !== "" ? data.data.records[i].postThumbnail : '/static/images/posts/6.jpg';
                                            indexblog =
                                                '<div class="home-article-wrapper clearfix">' +
                                                '<div class="home-article" data-cover="true">' +
                                                '<div class="home-article-inner" data-cover="true">' +
                                                '<div class="passage-meta">' +
                                                '<span>' +
                                                '<i class="fa fa-calendar"></i>' + fmt(data.data.records[i].postDate) + '' +
                                                '</span>' +
                                                '| <i class="fa fa-location-arrow"></i><a href="/web/categoryPage?categoryId=' + categorys.cateId + '"> ' + categorys.cateName + '' +
                                                '</a></span>' +
                                                '<span>' +
                                                '| <i class="fa fa-unlock-alt"></i>UNLOCK' +
                                                '</span>' +
                                                '</div>' +
                                                '<h1 class="home-article-title">' +
                                                '<a href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' + data.data.records[i].postTitle + '</a>' +
                                                '</h1>' +
                                                '<div class="home-article-content passage-article">' +
                                                '' + data.data.records[i].postContent.substring(0, 100) + '' +
                                                '</div>' +
                                                '<a class="home-article-read" href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' +
                                                '    README MORE >>>' +
                                                '</a>' +
                                                // '<div class="passage-tags">' +
                                                // '<a href="/tags/第三方库/"><i class="fa fa-tags"></i>第三方库</a>' +
                                                // '</div>' +
                                                '<div class="home-article-cover">' +
                                                '<img class="textimg" src="' + data.data.records[i].postThumbnail + '">' +
                                                '</div>' +
                                                '</div>' +
                                                '</div>';
                                            $("#home").append(indexblog);
                                        }

                                    }
                                } else {
                                    //layer.msg(data.msg, {time: 1 * 1000});
                                }
                            }
                        });
                        //首次不执行
                        if (!first) {
                            //do something
                        }
                    }
                });
            });
        }
    });

}

//显示坐标 @deprecated
function showpostion(num) {
    if (num % 2 === 0) {
        $("#aiticles").addClass("position: absolute; left: 390px; top: 0px;");
    }
}

//时间戳
function fmt(timestamp) {

    // var year = now.getFullYear();
    // var month = now.getMonth() + 1;
    // var date = now.getDate();
    // var hour = now.getHours();
    // var minute = now.getMinutes();
    // var second = now.getSeconds();
    // return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;

    //13位时间戳转换
    var date = new Date(timestamp / 1);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + " " + h + ":" + minute + ":" + second;
//10位时间戳转换
//             function timestampToTime2(timestamp) {
//     var date = new Date(timestamp * 1000);
//     var y = date.getFullYear();
//     var m = date.getMonth() + 1;
//     m = m < 10 ? ('' + m) : m;
//     var d = date.getDate();
//     d = d < 10 ? ('0' + d) : d;
//     var h = date.getHours();
//     h = h < 10 ? ('0' + h) : h;
//     var minute = date.getMinutes();
//     var second = date.getSeconds();
//     minute = minute < 10 ? ('0' + minute) : minute;
//     second = second < 10 ? ('0' + second) : second;
//     return y + '-' + m + '-' + d + " " + h + ":" + minute + ":" + second;
//             }
}
